package com.ptswitch.material.web.action;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;

import com.hxzon.util.EMailUtil;
import com.hxzon.util.OgnlUtil;
import com.hxzon.util.EMailUtil.EMail;
import com.ptswitch.material.model.Material;
import com.ptswitch.material.model.Upload;
import com.ptswitch.material.util.Constant;

@Controller("materialAction")
public class MaterialAction extends ActionSupport {

	private Material input;
	private String like;
	private String likeField;
	private Material likeMaterial;
	private boolean isNewPcbDecal;

	private EMail mail;

	// display
	public String display() {
		Material material = materialSer.get(id);
		if (material == null) {
			throw new RuntimeException("该物料不存在");
		}
		display.put("material", material);
		return "display";
	}

	public String all() {
		List materials = materialSer.getAll(page.getCurrentRow(), page.getPageSize());
		page.setRowCount(materialSer.countAll());
		display.put("materials", materials);
		putMsg("物料总数：" + page.getRowCount());
		return "list";
	}

	// to edit
	private EMail initEMail() {
		EMail mail = new EMail();
		mail.setHost("xxx");
		mail.setFrom("xxx");
		mail.setUsername("xxx");
		mail.setPassword("xxx");
		return mail;
	}

	public String toAdd() {
		mustLogin();
		input = null;
		mail = initEMail();
		display.put("emails", eMailAddressSer.getAll());
		return "add";
	}

	public String toUpdate() {
		mustAdmins();
		input = materialSer.get(id);
		mail = initEMail();
		display.put("emails", eMailAddressSer.getAll());
		return "update";
	}

	// save
	public String add() {
		mustLogin();
		if (input.getCode() == null || input.getCode().isEmpty()) {
			putMsg("保存物料失败：物料编码不能为空");
			return "add";
		}
		if (materialSer.getByCode(input.getCode()) != null) {
			putMsg("保存物料失败：物料编码已经存在");
			return "add";
		}
		String pcbDecal = input.getPcbDecal();
		if (isNewPcbDecal && materialSer.existPcbDecal(pcbDecal)) {
			putMsg("保存物料失败：物料的PCB封装不是新封装");
			display.put("material", input);
			input = null;
			return "add";
		}
		if (!isNewPcbDecal && !materialSer.existPcbDecal(pcbDecal)) {
			putMsg("保存物料失败：物料的PCB封装不存在");
			display.put("material", input);
			input = null;
			return "add";
		}

		materialSer.save(input);
		String overview = Constant.addMaterial + input.getCode();
		String message = hlogSer.buildMessage(input);
		hlogSer.log(visitor(), overview, message);
		mail.setSubject(visitor().getRealname() + overview);
		mail.setContentText(ActionSupport.getContextPath() + "/m!display.z?id=" + input.getId() + "\n"
				+ mail.getContentText() + "\n" + message);
		EMailUtil.send(mail, false);
		putMsg("保存物料成功");
		display.put("material", input);
		input = null;
		return "rDisplay";
	}

	public String update() {
		mustAdmins();
//		String pcbDecal = input.getPcbDecal();
//		if (isNewPcbDecal && materialSer.existPcbDecal(pcbDecal)) {
//			putMsg("保存物料失败：物料的PCB封装不是新封装");
//			display.put("material", input);
//			// input = null;
//			return "update";
//		}
//		if (!isNewPcbDecal && !materialSer.existPcbDecal(pcbDecal)) {
//			putMsg("保存物料失败：物料的PCB封装不存在");
//			display.put("material", input);
//			// input = null;
//			return "update";
//		}
		materialSer.save(input);
		String overview = Constant.updateMaterial + input.getCode();
		String message = hlogSer.buildMessage(input);
		hlogSer.log(visitor(), overview, message);
		mail.setSubject(visitor().getRealname() + overview);
		mail.setContentText(ActionSupport.getContextPath() + "/m!display.z?id=" + input.getId() + "\n"
				+ mail.getContentText() + "\n" + message);
		EMailUtil.send(mail, false);
		putMsg("保存物料成功");
		display.put("material", input);
		input = null;
		return "rDisplay";
	}

	// delete
	public String delete() {
		mustAdmins();
		Material material = (Material) display.get("material");
		if (!material.getBomMaterials().isEmpty()) {
			putMsg("被BOM表使用，不能删除");
			return "display";
		}
		for (Upload upload : material.getUploads()) {
			FileUtils.deleteQuietly(new File(ActionSupport.getSavePath() + upload.getFilePath()));
			uploadSer.delete(upload);
		}
		materialSer.delete(material);
		hlogSer.log(visitor(), Constant.deleteMaterial, hlogSer.buildMessage(material));
		return "rList";
	}

	public String deleteAll() {
		mustAdmin();
		materialSer.deleteAll();
		hlogSer.log(visitor(), Constant.deleteAllMaterial);
		return "rList";
	}

	// search
	public String like() {
		List materials = materialSer.getLike(like, page.getCurrentRow(), page.getPageSize());
		page.setRowCount(materialSer.countLike(like));
		display.put("materials", materials);
		return "list";
	}

	public String toSearch() {
		likeMaterial = new Material();
		putSession("likeMaterial",likeMaterial);
		return "search";
	}

	public String search() {
		likeMaterial = (Material)getSession("likeMaterial");
		List materials = materialSer.getLike(likeMaterial, page.getCurrentRow(), page.getPageSize());
		page.setRowCount(materialSer.countLike(likeMaterial));
		display.put("materials", materials);
//		likeMaterial = null;
		return "list";
	}

	public String same() {
		Material material = materialSer.get(id);
		if (material == null) {
		} else {
			String value = OgnlUtil.getStringValueOrEmpty(material, likeField);
			List materials = materialSer.getList(likeField, value, page.getCurrentRow(), page.getPageSize());
			page.setRowCount(materialSer.countList(likeField, value));
			display.put("materials", materials);
		}
		return "list";
	}

	// other
	public void emptyCode() {
		ajax(materialSer.emptyCode(id));
	}

	// get and set

	public Material getInput() {
		if (input == null) {
			input = new Material();
		}
		return input;
	}

	public String getLike() {
		return like;
	}

	public void setLike(String like) {
		this.like = like;
	}

	public String getLikeField() {
		return likeField;
	}

	public void setLikeField(String likeField) {
		this.likeField = likeField;
	}

	public Material getLikeMaterial() {
		return likeMaterial;
	}

	public EMail getMail() {
		return mail;
	}

	public boolean isNewPcbDecal() {
		return isNewPcbDecal;
	}

	public void setNewPcbDecal(boolean isNewPcbDecal) {
		this.isNewPcbDecal = isNewPcbDecal;
	}

}
